package com.efounder.form.comp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.efounder.builder.base.data.DataSetEvent;
import com.efounder.builder.base.data.DataSetListener;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.eai.EAI;
import com.efounder.form.DMColComponent;
import com.efounder.form.DMComponent;
import com.efounder.form.DataSetComponent;
import com.efounder.form.base.IComponent;
import com.efounder.form.util.FormCompUtil;
import com.efounder.frame.ViewSize;
import com.efounder.frame.utils.EFAppAccountUtils;
import com.efounder.imageloader.GlideImageLoader;
import com.efounder.mobilecomps.R;
import com.efounder.util.AppContext;

import static com.efounder.frame.utils.Constants.KEY_SETTING_DEFAULT_SERVER_ADDRESS;
import static com.efounder.frame.utils.Constants.KEY_SETTING_DEFAULT_SERVER_HTTP_TYPE;
import static com.efounder.frame.utils.Constants.KEY_SETTING_DEFAULT_SERVER_PATH;
import static com.efounder.frame.utils.Constants.KEY_SETTING_DEFAULT_SERVER_PORT;

/**
 * Created by Javor on 16/9/9.
 */

@SuppressLint("AppCompatCustomView")
public class FormImage extends ImageView implements IComponent, IUIComponent, DMComponent, DMColComponent, DataSetListener {
    public FormImage() {
        super(AppContext.getInstance());
    }

    public FormImage(Context context) {
        super(context);
    }

    public FormImage(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    private String id;
    private int height;
    private int width;

    private int gap = 0;
    private ViewSize size = new ViewSize(0, 0);
    private int percentWidth;
    private int percentHeight;
//    private ImageLoader imageLoader = ImageLoader.getInstance();
//    private DisplayImageOptions options;


    private EFRowSet mainRowSet;
    private String dataSetColID;
    private String dataSetID;
    EFDataSet dataSet;
    private IUIComponent parentComp;

    /**
     * 文件名：可以是配置的，可以是从数据中获取的(数据中获取的 > 配置的)  优先级：1
     **/
    private String imageName;
    /**
     * url前缀   优先级：2
     **/
    private String imageUrlPrefix;
    /**
     * 默认加载方式  本地：file 网络：http   优先级：3
     **/
    private String defaultImageLoadMode;

    @Override
    public String getID() {
        return id;
    }

    @Override
    public void setID(String id) {
        this.id = id;
    }

    @Override
    public void creationComplete() {
    }

    @Override
    public void setLayout() {
        FormCompUtil.setLayoutSize(this);
    }

    public void dataChanged(EFDataSet dataSet) {
//        /** 文件名：可以是配置的，可以是从数据中获取的(数据中获取的 > 配置的)  优先级：1**/
//        private String imageName;
//        /** url前缀   优先级：2**/
//        private String urlPrefix;
//        /** 默认加载方式  本地：file 网络：http   优先级：3**/
//        private String defaultImageLoadMode;

        /** 文件名：可以是配置的，可以是从数据中获取的(数据中获取的 > 配置的)  优先级：1**/

        //1.从数据中获取 imageName
        mainRowSet = dataSet.getCurrentRowSet();
        if (mainRowSet != null) {
            String imageData = mainRowSet.getString(dataSetColID, "");
            if (imageData == null || "".equals(imageData)) {
            } else {
                imageName = imageData;
            }
        }
        if (imageName != null && !"".equals(imageName)) {
            String imageUrl = null;
            if (imageName.contains(".")) {
                imageName = imageName.replace(imageName.substring(imageName.lastIndexOf("."), imageName.length()), "");
            }
            //2.判断imageName是否是url,如果是url，直接设置图片
            if (imageName.toLowerCase().startsWith("http://") || imageName.toLowerCase().startsWith("file://")) {
                imageUrl = imageName;
            } else if (imageUrlPrefix != null && !"".equals(imageUrlPrefix)) {/** url前缀   优先级：2**/
                imageUrl = imageUrlPrefix + "/" + imageName;
            } else if (defaultImageLoadMode != null && !"".equals(defaultImageLoadMode)) {/** 默认加载方式  本地：file 网络：http   优先级：3**/
                if ("http".equals(defaultImageLoadMode)) {
                    EAI.Protocol = EnvironmentVariable.getProperty(KEY_SETTING_DEFAULT_SERVER_HTTP_TYPE);
                    EAI.Server = EnvironmentVariable.getProperty(KEY_SETTING_DEFAULT_SERVER_ADDRESS);
                    EAI.Port = EnvironmentVariable.getProperty(KEY_SETTING_DEFAULT_SERVER_PORT);
                    EAI.Path = EnvironmentVariable.getProperty(KEY_SETTING_DEFAULT_SERVER_PATH);
                    imageUrl =  EAI.Protocol+"://"+EAI.Server+":"+ EAI.Port+"/"+ EAI.Path+"/APPS/image" + "/" + imageName;
                    //imageUrl = "http://mobile.osp.cloud/ESTMobile/APPS/image" + "/" + imageName;
                } else if ("file".equals(defaultImageLoadMode)) {
                    imageUrl = "file://" + EFAppAccountUtils.getAppAccountImagePath() + "/" + imageName;
                }
            }

            if (imageUrl != null) {
                mainRowSet.putString(dataSetColID, imageUrl);
                GlideImageLoader.getInstance().displayImage(getContext(),this,imageUrl);
//                imageLoader.displayImage(imageUrl, this);
            }
        } else {
            setImageResource(R.drawable.default_img);
        }
    }


    @Override
    public int getGap() {
        return gap;
    }

    @Override
    public void setGap(int gap) {
        this.gap = gap;
    }

    @Override
    public void setCompWidth(int width) {
        this.width = width;
    }

    @Override
    public int getCompWidth() {
        return this.width;
    }

    @Override
    public void setCompHeight(int height) {
        this.height = height;
    }

    @Override
    public int getCompHeight() {
        return this.height;
    }

    @Override
    public int getPercentWidth() {
        return percentWidth;
    }

    @Override
    public void setPercentWidth(int percentW) {
        percentWidth = percentW;
    }

    @Override
    public int getPercentHeight() {
        return percentHeight;
    }

    @Override
    public void setPercentHeight(int percentH) {
        percentHeight = percentH;
    }

    @Override
    public ViewSize getViewSize() {
        return size;
    }

    @Override
    public void setViewSize(ViewSize size) {
        this.size = size;
    }

    @Override
    public String getDataSetColID() {
        return null;
    }

    @Override
    public void setDataSetColID(String dataSetColID) {
        this.dataSetColID = dataSetColID;
    }

    @Override
    public String getDataSetNameColID() {
        return null;
    }

    @Override
    public void setDataSetNameColID(String s) {

    }

    @Override
    public String getInternalDataSetID() {
        return null;
    }

    @Override
    public void setInternalDataSetID(String s) {

    }

    @Override
    public void setViewDataSetID(String s) {

    }

    @Override
    public String getViewDataSetID() {
        return null;
    }

    @Override
    public void setViewDataSetColID(String s) {

    }

    @Override
    public String getViewDataSetColID() {
        return null;
    }

    @Override
    public void setValueDataSetColID(String s) {

    }

    @Override
    public String getValueDataSetColID() {
        return null;
    }

    @Override
    public Boolean getIsUserInternalDataSetID() {
        return null;
    }

    @Override
    public void setIsUserInternalDataSetID(Boolean aBoolean) {

    }

    @Override
    public String getIdentifier() {
        return null;
    }

    @Override
    public Object getPropertyMap() {
        return null;
    }

    @Override
    public void setPropertyMap(Object o) {

    }

    @Override
    public Object getProperty(String s, Object o) {
        return null;
    }

    @Override
    public void setProperty(String s, Object o) {

    }

    @Override
    public int getNumberPrecision() {
        return 0;
    }

    @Override
    public void seNumberPrecision(int i) {

    }

    @Override
    public String getNumberFormat() {
        return null;
    }

    @Override
    public void setNumberFormat(String s) {

    }

    @Override
    public String getColumnType() {
        return null;
    }

    @Override
    public void setColumnType(String s) {

    }

    @Override
    public String getEditorType() {
        return null;
    }

    @Override
    public void setEditorType(String s) {

    }

    @Override
    public String getDateFormat() {
        return null;
    }

    @Override
    public void setDateFormat(String s) {

    }

    @Override
    public String getFormulaOne() {
        return null;
    }

    @Override
    public void setFormulaOne(String s) {

    }

    @Override
    public String getEditMask() {
        return null;
    }

    @Override
    public void setEditMask(String s) {

    }

    @Override
    public String getFkeyID() {
        return null;
    }

    @Override
    public void setFkeyID(String s) {

    }

    @Override
    public String getRlglID() {
        return null;
    }

    @Override
    public void setRlglID(String s) {

    }


    @Override
    public DataSetComponent getDataSetComponent() {
        return null;
    }

    @Override
    public void setDataSetComponent(DataSetComponent dataSetComponent) {

    }

    @Override
    public void setDataSetID(String dataSetID) {
        this.dataSetID = dataSetID;
    }

    @Override
    public String getDataSetID() {
        return null;
    }

    @Override
    public void setDataSet(EFDataSet dataSet) {
        if (this.dataSet != dataSet) {
            if (this.dataSet != null) this.dataSet.removeDataSetListener(this);
            this.dataSet = dataSet;
            if (this.dataSet != null) this.dataSet.addDataSetListener(this);
            this.dataChanged(dataSet);
        }

    }

    @Override
    public EFDataSet getDataSet() {
        return null;
    }

    @Override
    public EFRowSet getMainRowSet() {
        return null;
    }

    @Override
    public void dataSetChanged(DataSetEvent dataSetEvent) {
        dataChanged(dataSetEvent.getDataSet());
    }


    @Override
    public IUIComponent getParentComp() {
        return parentComp;
    }

    @Override
    public void setParentComp(IUIComponent comp) {
        parentComp = comp;
    }


    public String getImageName() {
        return imageName;
    }

    public void setImageName(String imageName) {
        this.imageName = imageName;
    }

    public String getDefaultImageLoadMode() {
        return defaultImageLoadMode;
    }

    public void setDefaultImageLoadMode(String defaultImageLoadMode) {
        this.defaultImageLoadMode = defaultImageLoadMode;
    }

    public String getImageUrlPrefix() {
        return imageUrlPrefix;
    }

    public void setImageUrlPrefix(String imageUrlPrefix) {
        this.imageUrlPrefix = imageUrlPrefix;
    }
}


